package com.mediacloud.app.newsmodule.activity.audio

import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.mediacloud.app.assembly.util.load
import com.mediacloud.app.assembly.util.loadXWithRadiusGlide
import com.mediacloud.app.model.fragment.BaseFragment
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.model.news.kt.TypeX
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.fragment.baoliao.api.SpiderCmsApi
import com.mediacloud.app.newsmodule.utils.NewsItemClickUtils
import com.mediacloud.app.quanzi.model.BaseDataBean
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener
import com.scwang.smartrefresh.layout.listener.OnRefreshListener
import com.tencent.liteav.demo.superplayer.model.entity.AlbumRecommend
import kotlinx.android.synthetic.main.refresh_recyclerview_layout.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class AudioAlbumRecommendFragment : BaseFragment(), IScrollView, OnRefreshListener {
    var adapter: Adapter = Adapter()

    //var seriesRecommendDtos: ArrayList<SeriesRecommendDto>? = null
    var activity: AudioAlbumDetailActivity? = null
    override fun getLayoutResID(): Int {
        return R.layout.refresh_recyclerview_layout
    }

    private var articleID: String? = null

    override fun initView() {
        // seriesRecommendDtos = arguments?.getParcelableArrayList("RECOMMEND_LIST")
        articleID = arguments?.getString("ARTICLE_ID")
        getDataList(articleID)
        refreshLayout.setEnableLoadMore(false)
        refreshLayout.setEnableRefresh(true)
        recyclerView.layoutManager = LinearLayoutManager(requireContext())
        recyclerView.adapter = adapter
        //adapter.setNewData(seriesRecommendDtos)
        activity = requireActivity() as AudioAlbumDetailActivity
        adapter.setOnItemClickListener { adapter, view, position ->
            val rec = adapter.data.get(position) as AlbumRecommend
            val item = ArticleItem()
            item.type = TypeX.NEWS.AUDIO_COLLECTION;
            item.id = rec.relaArticleId
            NewsItemClickUtils.OpenItemNewsHandle(
                requireContext(),
                TypeX.NEWS.AUDIO_COLLECTION,
                item,
                activity?.catalogItem
            )
        }
    }


    private fun getDataList(articleID: String?) {
        SpiderCmsApi.getSpiderCmsApi(requireContext()).getSeriesRecommendByArticleId(articleID)
            .enqueue(object : Callback<BaseDataBean<List<AlbumRecommend>>> {
                override fun onResponse(
                    call: Call<BaseDataBean<List<AlbumRecommend>>>,
                    response: Response<BaseDataBean<List<AlbumRecommend>>>
                ) {
                    if (getActivity()==null||requireActivity().isFinishing||requireActivity().isDestroyed){
                        return
                    }
                    refreshLayout.finishRefresh()
                    if (response.isSuccessful){
                        val body = response.body()
                        if (body!=null&&body.isSuccess){
                            body.data?.let {
                                adapter.setNewData(it)
                            }
                        }
                    }
                }

                override fun onFailure(
                    call: Call<BaseDataBean<List<AlbumRecommend>>>,
                    t: Throwable
                ) {

                }

            })
    }

    inner class Adapter :
        BaseQuickAdapter<AlbumRecommend, BaseViewHolder>(R.layout.item_audio_album_list_recommend) {

        val url =
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.duoziwang.com%2F2019%2F07%2F12080849902758.jpg&refer=http%3A%2F%2Fimg.duoziwang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1659078440&t=4a40de2213fdb0f4d3c37575df8e155e"
        var radius = 0
        override fun convert(helper: BaseViewHolder, item: AlbumRecommend?) {
            val audioAlbumLogo = helper.getView<ImageView>(R.id.audioAlbumLogo)
            val audioAlbumTitleTv = helper.getView<TextView>(R.id.audioAlbumTitleTv)
            val audioAlbumPlayTimesTv = helper.getView<TextView>(R.id.audioAlbumPlayTimesTv)
            val audioAlbumSizeTv = helper.getView<TextView>(R.id.audioAlbumSizeTv)
            if (radius == 0) {
                radius = resources.getDimensionPixelSize(R.dimen.dimen6)
            }
            if(!TextUtils.isEmpty(item?.albumDto?.imagePath)){
                audioAlbumLogo?.loadXWithRadiusGlide(item?.albumDto?.imagePath, radius,null)
            }else{
                audioAlbumLogo?.loadXWithRadiusGlide(item?.albumDto?.logo, radius,null)
            }
            audioAlbumTitleTv.text = "${item?.albumDto?.name}"
            audioAlbumPlayTimesTv.text = "${item?.albumDto?.hitCount}"
            audioAlbumSizeTv.text = "${item?.albumDto?.articleCount}个音频"
        }

    }

    override fun getScrollView(): View {
        return recyclerView
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        getDataList(articleID)
    }


}